package com.cloudptt.wp.ui;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.cloudptt.api.product.Product;
import com.cloudptt.api.product.log.Log;
import com.cloudptt.wp.R;
import com.cloudptt.wp.ui.address.AddrGroupFragment;
import com.cloudptt.wp.ui.address.AddrUserFragment;
import com.cloudptt.wp.ui.address.IAddrFragmentUpdate;
import com.cloudptt.wp.ui.listen.IAddrUpdate;
import com.cloudptt.wp.ui.more.SettingActivity;
import com.google.android.material.tabs.TabLayout;
import com.mcptt.db.model.AddrInfo;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import com.cloudptt.wp.ui.address.adapter.MyPagerAdapter;

public class ContactFragment extends Fragment implements View.OnClickListener{

	private static String TAG = "ContactFragment";
	private Context ctx = null;
	private AddrInfo mcIdInfo = null;

	private View mRootView;

	//	private LinearLayout mEmptyLayout;
	private IAddrUpdate update = null;
	private int curGroupPos = 0;
	//	private GroupInfo defaultGroupInfo = null;
//	private RollbackTimer rollbackTimer = null;
	private Product product;

	private List<FragmentInfo> fragments = new ArrayList<>();

	public ContactFragment() {
	}

	public ContactFragment(Context ctx,AddrInfo mcIdInfo,IAddrUpdate update){
		this.ctx = ctx;
		this.mcIdInfo = mcIdInfo;
		this.update = update;
		AddrGroupFragment groupFg = new AddrGroupFragment(ctx,update,false,null);
		fragments.add(new FragmentInfo("群组",groupFg));
		AddrUserFragment userFg = new AddrUserFragment(ctx,false,false,null);
		fragments.add(new FragmentInfo("个人",userFg));
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		if(mRootView != null){
			return mRootView;
		}
		mRootView = inflater.inflate(R.layout.contact_fragment, container, false);
		ImageView settingsBtn = (ImageView)mRootView.findViewById(R.id.settingsBtn);
		settingsBtn.setOnClickListener(this);
		ViewPager viewPager = mRootView.findViewById(R.id.viewPager);
		TabLayout tabLayout = mRootView.findViewById(R.id.tabLayout);
//		tabLayout.setTabTextColors(R.color.white,R.color.black);
		for (int i = 0; i < tabLayout.getTabCount(); i++) {
			TabLayout.Tab tab = tabLayout.getTabAt(i);
			if (tab != null) {
				TextView tabView = (TextView) LayoutInflater.from(ctx).inflate(R.layout.tab_custom, null);
				tabView.setText(tab.getText());
				tab.setCustomView(tabView);
			}
		}

		// 自定义适配器继承 FragmentPagerAdapter（兼容旧版本）
		MyPagerAdapter adapter = new MyPagerAdapter(getActivity().getSupportFragmentManager(),fragments);
		viewPager.setAdapter(adapter);
		tabLayout.setupWithViewPager(viewPager);  // 关联两者 :ml-citation{ref="3,4" data="citationList"}

		return mRootView;
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		updateAddrList(false);
	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	public void onClick(View view) {
		Log.debug(this.getClass(),"onClick");
		switch (view.getId()) {
			case R.id.settingsBtn:
				Intent intent = new Intent(this.getActivity(),
						SettingActivity.class);
				startActivity(intent);
				this.getActivity().overridePendingTransition(R.anim.in_from_left, R.anim.out_to_right);
				break;
		}
	}

	public void updateAddrList(boolean isNotify){
		Log.debug(this.getClass(), "updateAddrList isNotify = " + isNotify);
		for (Iterator<FragmentInfo> itor = fragments.iterator();itor.hasNext();)
		{
			FragmentInfo fgInfo = itor.next();
			if(fgInfo != null
				&& fgInfo.getFg() != null)
			{
				IAddrFragmentUpdate update = (IAddrFragmentUpdate)fgInfo.getFg();
				update.updateAddrList(isNotify);
			}
		}
	}
}
